package battle

import (
	"server/game/cache"
	"server/game/define/helper"
	"server/share/config"

	"github.com/hwcer/yyds/context"
)

/**
 * @name startChallengeBattle
 * @param int id 关卡ID
 * 创建战区战斗
 */
func (this *newStage) StartChallengeBattle(c *context.Context) interface{} {
	//id := c.GetInt32("id")
	//if id == 0 {
	//	return errors.ErrArgEmpty
	//}
	//challengeConfig := config.Data.Challenge[id]
	//if challengeConfig == nil {
	//	return errors.ErrArgEmpty
	//}

	roleDoc := cache.GetRole(c.Player.Updater)
	roleAll := roleDoc.All()
	id := roleAll.BattlePoint + 1

	if challenge := config.GetChallenge(id); challenge == nil {
		return context.Error("已经通关所有关卡")
	}

	mw := &Middleware{}
	mw.Create(c.Player, MiddlewareTypeChallenge, id, nil)

	return 0
}

/**
 * @name endChallengeBattle
 * @param int id 关卡ID
 * 结束战区战斗
 */
func (this *newStage) EndChallengeBattle(c *context.Context) interface{} {

	roleDoc := cache.GetRole(c.Player.Updater)
	mw := &Middleware{}
	if !mw.Verify(c.Player, MiddlewareTypeChallenge, 0) {
		return c.Error("battle error")
	}
	id := mw.K

	challenge := config.GetChallenge(id)
	if challenge == nil {
		return context.Error("战区配置错误")
	}
	var rs string
	if id%10 == 0 {
		rs = challenge.BossReward
	} else {
		rs = challenge.FreeReward
	}
	//role := roleDoc.All()

	items := helper.AddItemsFromFormula(id, 1, rs)
	for k, v := range items {
		c.Player.Add(k, v)
	}

	//ra := slice.Multiple(rs, ",", ":")
	//for _, v := range ra {
	//	if len(v) >= 2 {
	//		c.Player.Add(v[0], v[1])
	//	}
	//}
	c.Player.Emit(config.Data.Events.BattleStage, id)
	roleDoc.Set("BattlePoint", id)
	return map[string]interface{}{"win": true}
}
